package com.mths.client.page;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Anchor;
import com.mths.client.component.Div;
import com.mths.client.constant.PageConstant;
import com.mths.client.controller.CompanyController;
import com.mths.client.controller.CompanyControllerAsync;
import com.mths.client.controller.LoginMenuController;
import com.mths.client.controller.LoginMenuControllerAsync;
import com.mths.client.dto.AuthDataDTO;
import com.mths.client.dto.MemberLoginDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.ClientCacheUtil;
import com.mths.client.util.PageUtil;
import com.mths.client.util.UrlUtil;

public class DashboardHeader extends AbstractPage implements IPage {

	private final LoginMenuControllerAsync	loginMenuController	= GWT.create(LoginMenuController.class);
	private final CompanyControllerAsync	companyController	= GWT.create(CompanyController.class);

	private static DashboardHeaderUiBinder	uiBinder			= GWT.create(DashboardHeaderUiBinder.class);

	interface DashboardHeaderUiBinder extends UiBinder<Widget, DashboardHeader> {
	}

	@UiField
	Anchor	loginLogoutAnc, loginUser;
	@UiField
	Image	logoImg;
	@UiField
	Div		logoImgDiv;

	public DashboardHeader() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		renderCompanyLogo();
		userLoginprocess();
	}

	private void loadComponent() {
		final String companyOid = ClientCacheUtil.instance().getSessionUser().getSelectedCompanyOid();
		companyController.companyHasLogo(companyOid, new AsyncCall<Boolean>() {
			public void successCall(Boolean result) {
				if (result) {
					logoImg.setUrl(UrlUtil.generateLogoViewUrl(companyOid));
				} else {
					logoImg.setUrl("images/company_logo.png");
				}
				logoImgDiv.add(logoImg);
			};

		});
		logoImg.setStyleName("maxH175");
	}

	public void userLoginprocess() {
		if (ClientCacheUtil.instance().getUserLogin()) {
			loginLogoutAnc.setText(messages.logout());
			loginUser.setVisible(true);
			loginUser.setText(ClientCacheUtil.instance().getLoginUserName());
		} else {
			loginLogoutAnc.setText("Üye Girişi");
			loginUser.setVisible(false);
		}
	}

	private void initEventHandler() {
		loginLogoutAnc.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (ClientCacheUtil.instance().getUserLogin()) {
					loginMenuController.logout(new MemberLoginDTO(), new AsyncCall<AuthDataDTO>() {

						@Override
						public void successCall(AuthDataDTO result) {
							ClientCacheUtil.instance().loginProcess(result, null);
						}

					});
				} else {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.MEMBER_LOGIN);
				}
			}
		});
	}

	public void renderCompanyLogo() {
		logoImgDiv.clear();
		loadComponent();
	}

	@Override
	public String pageName() {
		return null;
	}
}